package org.microser.gataway.zuul.system.service.impl;

import org.microser.gataway.zuul.system.entity.Permission;
import org.microser.gataway.zuul.system.entity.Role;
import org.microser.gataway.zuul.system.entity.RolePermission;
import org.microser.gataway.zuul.system.mapper.PermissionMapper;
import org.microser.gataway.zuul.system.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.microser.gataway.zuul.system.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liujiamiao
 * @since 2020-11-25
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 根据集合role找到所有权限
     * @param rolels
     * @return
     */
    @Override
    public List<Permission> permissions(List<Role> rolels) {
        Set<String> ids = new HashSet<>();
        rolels.stream().forEach(entity->{
            ids.addAll(rolePermissionService.queryPermisstionIdsByRole(entity));
        });
        List<Permission> permissions = baseMapper.selectBatchIds(ids);
        return permissions;
    }

    @Override
    public List<String> permissionValue(List<Permission> permissions) {
        //权限值
        List<String> permissionValue = permissions.stream().map(entity->{
            return entity.getPermisstionValue();
        }).filter(entity->{
            if(StringUtils.isEmpty(entity)){
                return false;
            }
            return true;
        }).collect(Collectors.toList());
        return permissionValue;
    }
}
